/*
 * Copyright (c) 2016 YCY Group Holding Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *
 * $Id: TopicSender.java  2016-7-7 下午3:16:53 jackLiang $
 */
package com.jack.mq.topic;

import javax.jms.DeliveryMode;
import javax.jms.MessageProducer;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicSession;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

/**
 * @author JackLiang
 * @date 2016-7-7
 * @version 1.0.0
 */
public class TopicSender {

    // 发送次数
    public static final int    SEND_NUM    = 5;

    // tcp 地址
    public static final String BROKER_URL  = "tcp://localhost:61616";

    // 目标，在ActiveMQ管理员控制台创建 http://localhost:8161/admin/queues.jsp
    public static final String DESTINATION = "jack.mq.topic";

    /**
     * <b>function:</b> 发送消息
     * 
     * @author hoojo
     * @createDate 2013-6-19 下午12:05:42
     * @param session 会话
     * @param publisher 发布者
     * @throws Exception
     */

    public static void sendMessage(TopicSession session, MessageProducer publisher) throws Exception {

        for (int i = 0; i < SEND_NUM; i++) {

            TextMessage messageText = session.createTextMessage();
            messageText.setText("message_" + System.currentTimeMillis());
            publisher.send(messageText);
            System.out.println("Send message: " + messageText.getText());
        }

    }

    public static void run() throws Exception {

        TopicConnection connection = null;

        TopicSession session = null;

        ActiveMQConnectionFactory factory = null;

        Topic topic = null;

        MessageProducer producer = null;
        try {

            // 创建链接工厂
            factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,
                                                    ActiveMQConnection.DEFAULT_PASSWORD, BROKER_URL);

            // 通过工厂创建一个连接
            connection = factory.createTopicConnection();

            // 启动连接
            connection.start();

            // 创建一个session会话
            session = connection.createTopicSession(Boolean.TRUE, TopicSession.AUTO_ACKNOWLEDGE);

            // 创建一个消息队列
            topic = session.createTopic(DESTINATION);

            // 创建消息发送者
            producer = session.createProducer(topic);

            // 设置持久化模式
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            //
            producer.setTimeToLive(10000);

            sendMessage(session, producer);
            //
            session.commit();
        } catch (Exception e) {
            throw e;
        } finally {

            // 关闭释放资源
            if (session != null) {
                session.close();
            }

            if (connection != null) {
                connection.close();

            }

        }

    }

    public static void main(String[] args) throws Exception {
        TopicSender.run();
    }
}
